package com.twu.biblioteca.util;


import com.twu.biblioteca.model.User;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class UserAuthenticator {
    protected static UserAuthenticator userAuthenticator;
    private static String activeUser = null;

    public static String getActiveUser() {
        return activeUser;
    }

    public static void setActiveUser(String activeUser) {
        UserAuthenticator.activeUser = activeUser;
    }

    public static void setActiveUser() {
        UserAuthenticator.activeUser = null;
    }

    private List<User> userList;

    static {
        userAuthenticator = new UserAuthenticator();
    }

    public UserAuthenticator() {
        userList = new ArrayList<User>();
        userList.add(new User("user1","pass1","email1","address1"));
        userList.add(new User("user2","pass2","email2","address2"));
        userList.add(new User("user3","pass3","email3","address3"));
    }

    public List<User> getUserList() {
        return userList;
    }


    public static boolean validate(String username, String password) {
        Iterator<User> iterator =  userAuthenticator.getUserList().iterator();
        while(iterator.hasNext()) {
            User user = iterator.next();
            if(user.getUsername().equalsIgnoreCase(username) && user.getPassword().equalsIgnoreCase(password)) {
                setActiveUser(username);
                return true;
            }
        }
        return false;
    }

    public static void logout(){
        setActiveUser();
    }
}
